package com.dkd.manage.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.dkd.common.utils.DateUtils;
import com.dkd.manage.domain.dto.ChannelDto;
import com.dkd.manage.domain.vo.ChannelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.ChannelMapper;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.service.IChannelService;

/**
 * 售货机货道Service业务层处理
 *
 * @author guowenjie
 * @date 2025-06-20
 */
@Service
public class ChannelServiceImpl implements IChannelService {
    @Autowired
    private ChannelMapper channelMapper;

    /**
     * 查询售货机货道
     *
     * @param id 售货机货道主键
     * @return 售货机货道
     */
    @Override
    public Channel selectChannelById(Long id) {
        return channelMapper.selectChannelById(id);
    }

    /**
     * 查询售货机货道列表
     *
     * @param channel 售货机货道
     * @return 售货机货道
     */
    @Override
    public List<Channel> selectChannelList(Channel channel) {
        return channelMapper.selectChannelList(channel);
    }

    /**
     * 新增售货机货道
     *
     * @param channel 售货机货道
     * @return 结果
     */
    @Override
    public int insertChannel(Channel channel) {
        channel.setCreateTime(DateUtils.getNowDate());
        return channelMapper.insertChannel(channel);
    }

    /**
     * 修改售货机货道
     *
     * @param channel 售货机货道
     * @return 结果
     */
    @Override
    public int updateChannel(Channel channel) {
        channel.setUpdateTime(DateUtils.getNowDate());
        return channelMapper.updateChannel(channel);
    }

    /**
     * 批量删除售货机货道
     *
     * @param ids 需要删除的售货机货道主键
     * @return 结果
     */
    @Override
    public int deleteChannelByIds(Long[] ids) {
        return channelMapper.deleteChannelByIds(ids);
    }

    /**
     * 删除售货机货道信息
     *
     * @param id 售货机货道主键
     * @return 结果
     */
    @Override
    public int deleteChannelById(Long id) {
        return channelMapper.deleteChannelById(id);

    }

    /**
     * 根据skuIds查询商品货道
     *
     * @param skuIds 商品id
     * @return 返回结果
     */
    @Override
    public int selectChannelsBySkuIds(Long[] skuIds) {
        return channelMapper.selectChannelsBySkuIds(skuIds);
    }

    /**
     * 根据售货机编号查询货道列表
     *
     * @param innerCode 售货机编号
     * @return 结果
     */
    @Override
    public List<ChannelVo> selectChannelListByInnerCode(String innerCode) {
        return channelMapper.selectChannelListByInnerCode(innerCode);
    }

    /**
     * 货道关联商品
     *
     * @param channelDto 货道配置参数
     * @return 结果
     */
    @Override
    public int channelConfig(ChannelDto channelDto) {
        //channelDto转为channel批量更新
        List<Channel> channelList = channelDto.getChannelList().stream().map(item -> {

            String innerCode = item.getInnerCode();
            String channelCode = item.getChannelCode();
            //根据售货机编号和货道编号查询货道信息
            Channel channel = channelMapper.selectChannelByInnerCodeAndChannelCode(innerCode, channelCode);
//            如果设备的货道不为空,设置设备货道的商品id,更新时间
            if (channel != null) {
                channel.setSkuId(item.getSkuId());
                channel.setUpdateTime(DateUtils.getNowDate());
            }

            return channel;
        }).collect(Collectors.toList());

        //批量更新
        return channelMapper.updateChannelBatch(channelList);
    }
}
